package com.client.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.client.pojo.Comments;
import com.client.pojo.Forums;
import com.client.service.CommentsService;
import com.client.service.ForumsService;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;


/**
 * <p>
 * 评论系统表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    CommentsService commentsService;
    @Autowired
    ForumsService forumsService;
    @RequestMapping("forumCommentAdd")
    ResponseMsg forumAdd(@RequestBody Comments comments){
        System.out.println(111111);
        boolean isSuccess = commentsService.save(comments);
        if (isSuccess){
            UpdateWrapper<Forums> wrapper = new UpdateWrapper<>();
            wrapper.eq("post_id",comments.getTargetId())
                    .setSql("comments = comments + 1");
            forumsService.update(wrapper);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"评论成功");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"评论失败");
    }


    //根据词条id获取5条父级和对应的用户信息,同时实现分页，不全部获取
    @PostMapping("/getCommentsByEntryId")
    public String getCommentsByEntryId(@RequestBody Map<String, Object> param){
        Integer entryId = (Integer) param.get("entryId");
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        
        // 计算偏移量：offset = (page - 1) * limit
        Integer offset = (page - 1) * limit;
        
        List<Comments> comments = commentsService.getCommentsByEntryId(entryId, offset, limit);
        
        return JSON.toJSONString(comments);
    }

    //根据评论id获取子评论列表
    @PostMapping("/getChildrenByCommentId")
    public String getChildrenByCommentId(@RequestBody Map<String, Object> param){
        Integer commentId = (Integer) param.get("commentId");
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        
        // 计算偏移量：offset = (page - 1) * limit
        //因为第一条查出来了，所以从第二条开始查
        Integer offset = (page - 2) * limit + 1;
        System.out.println("调用子评论查询"+"commentId:"+commentId+",page:"+page+",limit:"+limit+",offset:"+offset);
        
        List<Comments> children = commentsService.getChildrenByCommentId(commentId, offset, limit);
        System.out.println("子评论查询结果:"+children.toString());
        return JSON.toJSONString(children);
    }
    //增加词条评论
    @PostMapping("addComment")
    public String addComment(@RequestBody Map<String,Object> param, HttpServletRequest request){
        System.out.println("增加评论:"+param.toString());
        Comments comments = new Comments();
        comments.setTargetId((Integer) param.get("target_id")); // 修复字段名
        comments.setContent((String) param.get("content"));
        comments.setTargetType((String) param.get("target_type")); // 添加target_type
        comments.setParentCommentId((Integer) param.get("parent_comment_id")); // 添加parent_comment_id
        comments.setAuthorId((Integer) param.get("author_id")); // 添加author_id


        boolean isSuccess = commentsService.save(comments);
        if (isSuccess){
            return "评论成功";
        }
        return "评论失败";
    }
    //根据
}
